<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2018/9/2
 * Time: 20:12
 */

namespace Application\UserCenter\Controller;


use Application\Common\Rand;
use J\Exception;
use J\Intent;
use J\MVC\Controller;

class MockController extends Controller
{
    protected function beforeCore()
    {
        parent::beforeCore();
        $this->enableAdmin(self::AUTH_SUPER);
    }

    /**
     * 用户数据自动生成
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiUser(Intent $intent)
    {
        $rand = new Rand();
        $i = 0;
        while ($i++ < 80) {
            $this->getCollection('user')->getCurd()->setElements([
                'name' => str_random(8, false),
                'nickname' => $rand->getChinaName(),
                'createTime' => $rand->time(),
                'password' => Password::generate('111111'),
                'ip' => $rand->getIp(),
                'updateTime' => $rand->time(),
                'lastIp' => $rand->getIp(),
                'lastTime' => $rand->time(),
                'status' => 1
            ])->add();
        }
        $intent->ajax('true', '用户数据生成完成');
    }

    /**
     * 用户数据自动生成
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiRole(Intent $intent)
    {
        $roles = [
            '帮主' => '帮会最高领导者（总权限）',
            '长老' => '帮会高层领导（部分权限）',
            '护法' => '帮会专职领导（专项权限）',
            '堂主' => '分堂总管（部门权限）',
            '香主' => '分堂副总管（部门部分权限）',
            '精英帮众' => '分堂精英（部门特别开放权限）',
            '普通帮众' => '未入分堂（开放权限）',
        ];

        foreach ($roles as $role => $description) {
            $this->getCollection('role')->getCurd()->setElements([
                'title' => $role,
                'description' => $description,
                'status' => 1
            ])->add();
        }
        $intent->ajax('true', '角色数据生成完成');
    }

    /**
     * 授权数据生成
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiPower(Intent $intent)
    {
        $powers = [
            [2, 2], [2,6], [3,2], [3,7], [4,3], [5,4], [6,4]
        ];
        foreach ($powers as $role => $resource) {
            $this->getCollection('user_role')->getCurd()->setElements([
                'user' => $resource[0],
                'role' => $resource[1],
                'status' => 1
            ])->add();
        }
        $intent->ajax('true', '授权数据生成完成');
    }
}